package com.atomic.test.example.controller;

import com.atomic.test.example.entity.User;
import com.atomic.test.example.service.IUserService;
import com.atomic.test.example.utils.UserContext;
import com.atomic.test.example.common.ErrorCode;
import com.atomic.test.example.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.atomic.test.example.utils.CheckDataUtil.isChinese;
import static com.atomic.test.example.utils.CheckDataUtil.updateUserCheck;
import static com.atomic.test.example.common.Constants.CLIENT_SECRET;
import static com.atomic.test.example.common.ErrorCode.STATUS_SUCCESS;
import static com.atomic.test.example.common.ErrorCode.USER_USERPWD_EMPTY;

/**
 * Created by DreamYao on 2017/1/24.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户注册
     *
     * @param user
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Boolean> save(@RequestBody User user) {
        String userName = user.getUserName();
        String password = user.getPassword();
        if (userName == null || userName.equals("") || password == null || password.equals("")) {
            return new Result<>(USER_USERPWD_EMPTY, "用户或密码为空");
        }
        if (!isChinese(userName) || !isChinese(password)) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        userService.save(user);
        return new Result<>();
    }

    /**
     * 用户删除
     *
     * @param user {"userName":""}
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Void> deleteUser(@RequestBody User user) {

        // 从用户上下文中获取登录的用户
        User loginUser = UserContext.getLoginUser();
        Integer loginUserId = loginUser.getId();
        String userName = user.getUserName();
        User queryUser = userService.getUserByUserName(userName);
        Integer deleteUserId = queryUser.getId();
        if (loginUserId.equals(deleteUserId)) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "用户不能删除自己!");
        }
        userService.delete(deleteUserId);
        return new Result<>(STATUS_SUCCESS, "删除用户成功!");
    }

    /**
     * 管理员更新用户信息(包括密码修改、用户锁定、用户解锁、积分修改)
     *
     * @param
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<Void> updateUser(@RequestBody User user) {

        if (!updateUserCheck(user)) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误!");
        }
        userService.update(user);
        return new Result<>();
    }

    /**
     * 查询所有用户信息
     * 分页功能待实现
     * @param
     */
    @ResponseBody
    @RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    public Result<List<User>> getAllUsers(){

        List<User> users;
        users = userService.getAllUsers();
        for (User user : users) {
            user.setPassword(CLIENT_SECRET);
        }
        return new Result<>(users);
    }

    /**
     * 用户密码更新
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public Result<Void> updatePassword(@RequestBody User user){
        if (user.getUserName() == null || user.getUserName().equals("")
                || user.getPassword() == null || user.getPassword().equals("")) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        userService.updatePassword(user);
        return new Result<>();
    }
}
